<?php namespace App\Models;

use Illuminate\Database\Eloquent\SoftDeletes;
use Watson\Validating\ValidatingTrait;
use Illuminate\Database\Eloquent\Model;

/**
 * 报名表
 * Class EntryForm
 *
 * @package App\Models
 * @property int $id
 * @property int $user_id 用户id
 * @property int $school_dept_id 学校院系id
 * @property string $major 专业
 * @property string $address 地址
 * @property int $is_completed 是否完成报名
 * @property string $created_at
 * @property string $updated_at
 * @property string $deleted_at
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Author[] $authors
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Teacher[] $teachers
 * @property-read \App\Models\SchoolDept $schoolDept
 * @method static \Illuminate\Database\Query\Builder|\App\Models\EntryForm whereId($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\EntryForm whereUserId($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\EntryForm whereSchoolDeptId($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\EntryForm whereMajor($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\EntryForm whereIsCompleted($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\EntryForm whereCreatedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\EntryForm whereUpdatedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\EntryForm whereDeletedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\EntryForm whereAddress($value)
 * @mixin \Eloquent
 */
class EntryForm extends Model
{
    use SoftDeletes;
    use ValidatingTrait;

    protected $table = 'entry_forms';

    protected $dates = ['deleted_at'];

    protected $fillable = ['school_dept_id', 'major', 'address'];

    protected $rules = [
        'user_id'        => 'required|integer|min:1',
        'school_dept_id' => 'required|integer|min:1',
        'major'          => 'required',
        'address'        => 'required',
    ];

    protected $validationMessages = [
        'user_id.required'        => '用户id必填',
        'user_id.min'             => '用户id必填',
        'user_id.integer'         => '用户id格式有误',
        'school_dept_id.required' => '院系必填',
        'school_dept_id.min'      => '院系必填',
        'school_dept_id.integer'  => '院系格式有误',
        'major.required'          => '系/专业必填',
        'address.required'        => '地址必填',
    ];

    /**
     * 所有作者
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function authors()
    {
        return $this->hasMany('\App\Models\Author', 'entry_form_id');
    }

    /**
     * 所有教师
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function teachers()
    {
        return $this->hasMany('\App\Models\Teacher', 'entry_form_id');
    }

    /**
     * 所属院系
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function schoolDept()
    {
        return $this->belongsTo('\App\Models\SchoolDept', 'school_dept_id');
    }

    /**
     * 设置完成状态
     */
    public function setCompleteStatus()
    {
        $unCompleteAuthors = Author::where('entry_form_id', $this->id)->whereNull('sign_pic')->count();

        if ($unCompleteAuthors) {
            $this->is_completed = 0;
        } else {
            if ($this->authors->count() && $this->teachers->count()) {
                $this->is_completed = 1;
            } else {
                $this->is_completed = 0;
            }
        }
        $this->save();
    }
}